package cn.com.guage.guc.juc;

import java.util.concurrent.Semaphore;
/**
 * 
 * @author yangdechao
 * 5个窗口，10个人买票模拟
 */

public class SemaphoreTicketDemo {

	public static void main(String[] args) {
		Semaphore windows = new Semaphore(5); // 声明5个窗口

		for (int i = 0; i < 10; i++) {
			new Thread(()->{
				try {
					windows.acquire(); // 占用窗口
					System.out.println(Thread.currentThread().getName() + ": 开始买票");
					Thread.sleep(2000); // 睡2秒，模拟买票流程
					System.out.println(Thread.currentThread().getName() + ": 购票成功");
					windows.release(); // 释放窗口
 				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			},"第"+String.valueOf(i)+"个人").start();
		}
	}

}
